package com.android.RssReader.Utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.android.RssReader.RssReaderApplication;

public class XmlBuildUtil {
	
	/*登陆/注册
	 * <stream:stream to="10.0.2.2" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0">
		<iq id=”b5wUu-1” type=”set”>
		<cmd type=”login”>
		<username>jack</username>用户名
		<password>123456</password>密码
		<imei>1234567</imei>移动设备识别码
		<imsi>1234567</imsi>移动用户识别码
		<iccid>1234567</iccid>sim卡号
		<resource>AndroidpnClient</resource>
		</cmd>
		</iq>
	 * <cmd type =”login” > 
	 * <username></username>；用户名 
	 * <password></password> ；密码
	 * < imei ></ imei > ;移动设备识别码 
	 * < imsi ></ imsi >；移动用户识别码 
	 * < iccid ></ iccid>；sim卡号 
	 * </ cmd >
	 */

//	public static void buildLoginXML(OutputStream outputStream) throws Exception {
//		XmlSerializer serializer = Xml.newSerializer();
//		serializer.setOutput(outputStream, "utf-8");
//
//		serializer.startDocument("utf-8", true);
//		
//		serializer.startTag(null, "iq");
//		serializer.attribute(null, "id", "b5wUu-1");
//		serializer.attribute(null, "type", "set");
//		
//		serializer.startTag(null, "cmd");
//		serializer.attribute(null, "type", "login");
//
//		serializer.startTag(null, "username");
//		serializer.text("mao");
//		serializer.endTag(null, "username");
//
//		serializer.startTag(null, "password");
//		serializer.text("123");
//		serializer.endTag(null, "password");
//
//		serializer.startTag(null, "imei");
//		serializer.text(DeviceInformation
//				.getInformation(DeviceInformation.InfoName.IMEI));
//		serializer.endTag(null, "imei");
//
//		serializer.startTag(null, "imsi");
//		serializer.text(DeviceInformation
//				.getInformation(DeviceInformation.InfoName.PHONE_MODEL));
//		serializer.endTag(null, "imsi");
//
//		serializer.startTag(null, "iccid");
//		serializer.text(DeviceInformation
//				.getInformation(DeviceInformation.InfoName.CPU_MODEL));
//		serializer.endTag(null, "iccid");
//
//		serializer.endTag(null, "cmd");
//		serializer.endTag(null, "iq");
//		serializer.endDocument();
//		//outputStream.close();
//		String str = outputStream.toString();  
//		RssReaderApplication.LogD(XmlBuildUtil.class, str);
//	}
	
	public static byte[] buildLoginXML() throws Exception {
//		XmlSerializer serializer = Xml.newSerializer();
//		StringWriter writer = new StringWriter();  
//		serializer.setOutput(writer);
//		serializer.startDocument("utf-8", true);
//		
//		serializer.startTag(null, "iq");
//		serializer.attribute(null, "id", "b5wUu-1");
//		serializer.attribute(null, "type", "set");
//		
//		serializer.startTag(null, "cmd");
//		serializer.attribute(null, "type", "login");
//
//		serializer.startTag(null, "username");
//		serializer.text("mao");
//		serializer.endTag(null, "username");
//
//		serializer.startTag(null, "password");
//		serializer.text("123");
//		serializer.endTag(null, "password");
//
//		serializer.startTag(null, "imei");
//		serializer.text(DeviceInformation
//				.getInformation(DeviceInformation.InfoName.IMEI));
//		serializer.endTag(null, "imei");
//
//		serializer.startTag(null, "imsi");
//		serializer.text(DeviceInformation
//				.getInformation(DeviceInformation.InfoName.IMSI));
//		serializer.endTag(null, "imsi");
//
//		serializer.startTag(null, "iccid");
//		serializer.text(DeviceInformation
//				.getInformation(DeviceInformation.InfoName.ICCID));
//		serializer.endTag(null, "iccid");
//
//		serializer.endTag(null, "cmd");
//		serializer.endTag(null, "iq");
//		serializer.endDocument();
//		writer.flush();  
//        writer.close();  
//		String str = writer.toString(); 
//		RssReaderApplication.LogD(XmlBuildUtil.class, str);
//		byte[] data = str.getBytes("UTF-8");//得到了xml的实体数据 
//		return data;
		InputStream in = RssReaderApplication.getInstance().getResources().getAssets().open("test.xml");
		byte[] data = readStream(in);   
		return data;
	}
	
	 //通过输入流获得字节数组   
    public static byte[] readStream(InputStream is) throws Exception {   
        byte[] buffer = new byte[1024];   
        ByteArrayOutputStream bos = new ByteArrayOutputStream();   
        int len = 0;   
        while((len=is.read(buffer)) != -1){   
            bos.write(buffer, 0, len);   
        }    
        is.close();   
        return bos.toByteArray();   
    }   

	
	 /** 
     * 发送xml数据 
     * @param path 请求地址 
     * @param xml xml数据 
     * @param encoding 编码 
     * @return 
     * @throws Exception 
     */  
    public static byte[] postXml(String path, String encoding) throws Exception{  
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
        conn.setRequestMethod("POST");  
        conn.setDoOutput(true);  
        byte[] data = buildLoginXML();
        //conn.setConnectTimeout(5000);  
      
        conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");  
      
        conn.setRequestProperty("Content-Length", String.valueOf(data.length));  

        OutputStream outStream =  conn.getOutputStream();  
      
        outStream.write(data);  
      
        outStream.flush();  
      
        outStream.close();  
        
        if(conn.getResponseCode()==200){  
        	RssReaderApplication.LogD(XmlBuildUtil.class, "response:" + inputStream2String(conn.getInputStream()));
        }  else {
        	RssReaderApplication.LogD(XmlBuildUtil.class, "response:error");
        }
        return null;  
    }  
    
    
    private static String inputStream2String(InputStream is){
    	   BufferedReader in = new BufferedReader(new InputStreamReader(is));
    	   StringBuffer buffer = new StringBuffer();
    	   String line = "";
    	   try {
			while ((line = in.readLine()) != null){
			     buffer.append(line);
			   }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	   return buffer.toString();
    	} 
    
}
